package com.cgy.utils.jmx.jvm.thread;

import com.cgy.utils.jmx.IJvmInfoFactory;
import com.cgy.utils.jmx.jvm.utils.DateUtils;

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;

/**
 * @author CHTK
 */
public class ThreadFatory extends IJvmInfoFactory {


    @Override
    public void status() {
        ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
        put("thread.size", threadMXBean.getThreadCount(), "线程数");
        put("thread.daemonsize", threadMXBean.getDaemonThreadCount(), "daemon线程数");
        long[] allThreadIds = threadMXBean.getAllThreadIds();
        for (long allThreadId : allThreadIds) {
            ThreadInfo threadInfo = threadMXBean.getThreadInfo(allThreadId);

            com.cgy.utils.jmx.jvm.constant.ThreadInfo threadInfo1 = new com.cgy.utils.jmx.jvm.constant.ThreadInfo();
            threadInfo1.setThreadCpuTime(threadMXBean.getThreadCpuTime(allThreadId));
            threadInfo1.setThreadUserTime(threadMXBean.getThreadUserTime(allThreadId));
            threadInfo1.setLockName(threadInfo.getLockName());
            threadInfo1.setThreadId(threadInfo.getThreadId());
            threadInfo1.setLockName(threadInfo.getThreadName());
            threadInfo1.setThreadUserString(DateUtils.formatTime(threadMXBean.getThreadUserTime(allThreadId) * 1000));
            threadInfo1.setThreadCpuString(DateUtils.formatTime(threadMXBean.getThreadCpuTime(allThreadId) * 1000));

            put(threadInfo.getThreadName(), threadInfo1, "线程信息");
        }
    }
}
